package com.example.lovedogs.fragment.zizhen;

import java.lang.reflect.Type;
import java.util.List;

import com.example.lovedogs.R;
import com.example.lovedogs.domain.SickInfoBdetails;
import com.example.lovedogs.domain.SickInfoDisease;
import com.example.lovedogs.domain.SickInfoSdetails;
import com.example.lovedogs.zizhen.SickInfoDiseaseActivity;
import com.example.lovedogs.zizhen.SickInfoStateActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SickInfoStateFragmentRight extends Fragment {
	// private String
	// url="http://10.40.7.187:8080/LoveDogs04/JsonSickInfoSdetailsServlet";
	private String url;
	private View view;
	private ListView lv;
	private SdetailsAdapter adapter;
	private BdetailsAdapter bAdapter;
	private LayoutInflater inflater;
	private SickInfoStateActivity currentactivity;
	private int classno = 0;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.inflater = inflater;
		currentactivity = (SickInfoStateActivity) getActivity();
	

		if (view == null) {
			view = inflater.inflate(R.layout.fragment_sickinfostate_right,
					container, false);
			lv = (ListView) view.findViewById(R.id.lv_sickinfosdetails);
			String info=currentactivity.getIntent().getStringExtra("info");
			if("state".equals(info)){
				url = currentactivity.getString(R.string.serverurl)
						+ "JsonSickInfoSdetailsServlet";
				lv.setOnItemClickListener(new SdetailsOnItemClickListener());
			}
			if("body".equals(info)){
				url = currentactivity.getString(R.string.serverurl)
						+ "JsonSickInfoBdetailsServlet";
				lv.setOnItemClickListener(new BdetailsOnItemClickListener());
				
			}
			
		}

		return view;
	}

	public void updateSdetails(int stateNo) {
		httpSdetails(stateNo);
	}
	public void updateBdetails(int stateNo) {
		httpBdetails(stateNo);
		
	}


	public void httpSdetails(int stateNo) {
		RequestParams params = new RequestParams();
		params.addBodyParameter("classno", String.valueOf(stateNo));
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				Gson g = new Gson();
				Type type = new TypeToken<List<SickInfoSdetails>>() {
				}.getType();
				System.out.println(responseInfo.result);
				List<SickInfoSdetails> data = g.fromJson(responseInfo.result,
						type);
				if (adapter == null) {
					adapter = new SdetailsAdapter(data);
				} else {
					adapter.setData(data);
				}
				adapter.notifyDataSetChanged();
				lv.setAdapter(adapter);

			}

			@Override
			public void onFailure(HttpException error, String msg) {

			}
		});
	}
	public void httpBdetails(int stateNo) {
		RequestParams params = new RequestParams();
		params.addBodyParameter("classno", String.valueOf(stateNo));
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {
			
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				Gson g = new Gson();
				Type type = new TypeToken<List<SickInfoBdetails>>() {
				}.getType();
				System.out.println(responseInfo.result);
				List<SickInfoBdetails> data = g.fromJson(responseInfo.result,
						type);
				if (bAdapter == null) {
					bAdapter = new BdetailsAdapter(data);
				} else {
					bAdapter.setData(data);
				}
				bAdapter.notifyDataSetChanged();
				lv.setAdapter(bAdapter);
				
			}
			
			@Override
			public void onFailure(HttpException error, String msg) {
				
			}
		});
	}

	private class SdetailsAdapter extends BaseAdapter {
		private List<SickInfoSdetails> data;

		public SdetailsAdapter(List<SickInfoSdetails> data) {
			this.data = data;
		}

		public List<SickInfoSdetails> getData() {
			return data;
		}

		public void setData(List<SickInfoSdetails> data) {
			this.data = data;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			SickInfoSdetails info = data.get(position);
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.textview_item, null);
			}
			TextView tv = (TextView) convertView.findViewById(R.id.tv_item);
			tv.setText(info.getName());
			tv.setPadding(20, 20, 20, 20);
			return convertView;
		}

	}
	
	
	private class BdetailsAdapter extends BaseAdapter {
		private List<SickInfoBdetails> data;
		
		public BdetailsAdapter(List<SickInfoBdetails> data) {
			this.data = data;
		}
		
		public List<SickInfoBdetails> getData() {
			return data;
		}
		
		public void setData(List<SickInfoBdetails> data) {
			this.data = data;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}
		
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return data.get(position);
		}
		
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			SickInfoBdetails info = data.get(position);
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.textview_item, null);
			}
			TextView tv = (TextView) convertView.findViewById(R.id.tv_item);
			tv.setText(info.getName());
			tv.setPadding(20, 20, 20, 20);
			return convertView;
		}
		
	}
	
	
	private class SdetailsOnItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			SickInfoSdetails demo=(SickInfoSdetails) adapter.getItem(position);
			Intent intent=new Intent(currentactivity, SickInfoDiseaseActivity.class);
			intent.putExtra("keyword", demo.getName());
			startActivity(intent);
		}
	}
	private class BdetailsOnItemClickListener implements OnItemClickListener{
		
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			SickInfoBdetails demo=(SickInfoBdetails) bAdapter.getItem(position);
			Intent intent=new Intent(currentactivity, SickInfoDiseaseActivity.class);
			intent.putExtra("keyword", demo.getName());
			startActivity(intent);
		}
	}


	
}
